package edu.hm.devcamp.presentation;

import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import edu.hm.devcamp.R;
import edu.hm.devcamp.presentation.overlays.PoiOverlay;
import edu.hm.devcamp.providers.Poi;
import edu.hm.devcamp.util.GeoUtils;

public class LocationListenerImpl implements LocationListener {

    private Context mContext;
	private MapView mapView;
	private List<Poi> pois;
		
	public static GeoPoint currentLocation = null;
	public static PoiOverlay currentPositionOverlay = null;
	
	public LocationListenerImpl(MapView mv, Context context) {
        mContext = context;
        this.mapView = mv;
        currentLocation = new GeoPoint((int)(48.0019 * 1e6), (int)(11.382 * 1e6));
    }
	
	public LocationListenerImpl(List<Poi> pois, Context context) {
        mContext = context;
        this.pois = pois;
        currentLocation = new GeoPoint((int)(48.0019 * 1e6), (int)(11.382 * 1e6));
    }
    
	@Override
	public void onLocationChanged(Location location) {
		
		double lat = location.getLatitude();
		double lng = location.getLongitude();
		
		currentLocation = new GeoPoint((int)(lat * 1e6),
									   (int) (lng *1e6));
		
		if(mapView != null){
    		MapController mc = mapView.getController();
    		List<Overlay> list = mapView.getOverlays();
		
		
    		// we don't want the old positoin to appears
    		if(currentPositionOverlay != null && list.contains(currentPositionOverlay)){
    		    list.remove(currentPositionOverlay);
    		}
    		
    		Drawable marker = mContext.getResources().getDrawable(R.drawable.ic_maps_indicator_current_position);
    		currentPositionOverlay = new PoiOverlay(marker, mContext);
    		OverlayItem item = new OverlayItem(currentLocation, mContext.getString(R.string.current_position), "");
    		currentPositionOverlay.addOverlay(item);
    				
    		list.add(currentPositionOverlay);
    		currentPositionOverlay.populateOverlays();
    		
    		mc.animateTo(currentLocation);	
		} else if (pois != null) {
		    // if there's no map overlay, we calculate the distances of the pois to the device
		    GeoUtils.updatePoiListDistance(pois, currentLocation);
		}
	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub

	}

	public void moveToPosition() {
		MapController mc = mapView.getController();
		mc.animateTo(currentLocation);		
	}
	
}
